package com.lkd.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lkd.dto.SkuClassDto;
import com.lkd.entity.SkuClass;
import com.lkd.result.PageBean;
import com.lkd.result.Result;
import com.lkd.service.SkuClassService;
import com.lkd.vo.SkuClassVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/vm-service/skuClass")
public class SkuClassController {
    @Autowired
    private SkuClassService skuClassService;

    /**
     * 新增分类
     * @param skuClassDto
     * @return
     */
    @PostMapping
    public Result save(@RequestBody SkuClassDto skuClassDto) {
        SkuClass skuClass = BeanUtil.toBean(skuClassDto, SkuClass.class);
        skuClassService.save(skuClass);
        return Result.success(true);
    }

    /**
     * 分页查询分类
     * @param pageIndex
     * @param pageSize
     * @param className
     * @return
     */
    @GetMapping("/search")
    public Result pageConditonQuerySkuClass(
            @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "className", required = false) String className
    ) {
        PageBean<SkuClass> pageBean = skuClassService.pageConditonQuerySkuClass(pageIndex, pageSize, className);
        return Result.success(pageBean);
    }

    /**
     * 根据id查询分类基本信息
     * @param classId
     * @return
     */
    @GetMapping("/{classId}")
    public Result getById(@PathVariable("classId") Integer classId) {
        SkuClassVo skuClass = skuClassService.getById(classId);
        return Result.success(skuClass);
    }

    /**
     * 修改分类
     * @param skuClassDto
     * @return
     */
    @PutMapping
    public Result updateById(@RequestBody SkuClassDto skuClassDto) {
        SkuClass skuClass = BeanUtil.toBean(skuClassDto, SkuClass.class);
        skuClassService.updateById(skuClass);
        return Result.success(true);
    }

    /**
     * 删除分类
     * @param classId
     * @return
     */
    @DeleteMapping("/{classId}")
    public Result deleteById(@PathVariable("classId") String classId) {
        skuClassService.deleteById(classId);
        return Result.success(true);
    }
}